<?php

namespace App\Backend\Http\Controllers;

use App\App;
use App\Appcat;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

class AppController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
        $data['apps'] = App::orderBy('id','desc')->get();
        return view('backend::app.app',$data);
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create($id)
    {
        $data['cats'] = Appcat::orderBy('id','desc')->get();
        $data['app'] = App::find($id);
        return view('backend::app.appcreate',$data);
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        $data = $request->validate([
                'appcatid'=> 'required',
                'title'=> 'required',
                'logo'=> 'required',
                'time'=> 'required',
                'size'=> 'required',
                'version'=> 'required',
                'supportsystem'=> 'required',
                'url'=> 'required',
                'downurl'=> 'required',
                'content'=> 'required',
            ]);
        if($request->id){
            App::where('id',$request->id)->update($data);
        }else{
            unset($data['id']);
            App::create($data);
        }
        return redirect('/backend/app');
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('backend::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('backend::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(Request $request)
    {
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
        App::where('id', $id)->delete();
        return redirect('/backend/app');
    }
}
